package com.yantong.controller.api.xcxapi;

import com.yantong.pojo.bo.UserInfoBo;
import com.yantong.result.ResultVo;
import com.yantong.service.xcx.XCXLoginService;
import com.yantong.utils.AesCbcUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 小程序登录相关
 * @author: juzi
 * @date: 2021/12/2
 * @time: 9:21
 */
@Api(tags = "小程序登录相关")
@Controller
@RequestMapping("/xcxapi/login")
public class XCXLoginController {

    @Autowired
    XCXLoginService xcxLoginService;

    /**
     * 授权登录
     * @param centreId
     * @param code
     * @return
     */
    @ApiOperation(value = "登录授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "centreId", value = "商户Id", defaultValue = "", dataType = "long", required = true),
            @ApiImplicitParam(name = "memberId", value = "门店Id", defaultValue = "", dataType = "long", required = true),
            @ApiImplicitParam(name = "code", value = "微信code", defaultValue = "", dataType = "String", required = true),
    })
    @ResponseBody
    @GetMapping("/auth")
    public ResultVo login(Long centreId,Long memberId, String code) {
        if(null == centreId || StringUtils.isEmpty(code)){
            return ResultVo.error("商户或微信code不能为空");
        }
        return xcxLoginService.getTokenByCentreIdAndCode(centreId,memberId,code);
    }

    /**
     * 注册逻辑
     * @return
     */
    @ApiOperation(value = "注册")
    @ResponseBody
    @PostMapping("/register")
    public ResultVo register(@RequestBody @Valid UserInfoBo userInfoBo) {
        return xcxLoginService.register(userInfoBo);
    }



    /**
     * 解密数据
     * @return
     */
    @ApiOperation(value = "解密数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "加密数据", defaultValue = "", dataType = "String", required = true),
            @ApiImplicitParam(name = "key", value = "key", defaultValue = "", dataType = "String", required = true),
            @ApiImplicitParam(name = "iv", value = "iv", defaultValue = "", dataType = "String", required = true),
    })
    @ResponseBody
    @GetMapping("/jiemi")
    public ResultVo jiemi(String data, String key,String iv) throws Exception{
        return ResultVo.ok(AesCbcUtil.decrypt(data,key,iv,"UTF-8"));
    }


}
